package com.admin.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.admin.bean.AdminIvrStatFeeBean;
import com.admin.bean.AdminSpBean;
import com.admin.bean.AdminStatFeeBean;
import com.admin.bean.AdminUserBean;
import com.admin.bean.CompanyBean;
import com.admin.service.AdminIvrStatFeeService;
import com.admin.service.AdminSpService;
import com.admin.service.AdminUserService;
import com.admin.service.CompanyService;
import com.admin.service.ProvinceService;
import com.admin.util.BaseUtil;
import com.admin.util.BillingUtil;
import com.admin.util.DateUtil;
import com.admin.util.PageBean;


@Controller
@RequestMapping("/ivr")
public class IvrAction {

	@Autowired private CompanyService companyService;
	@Autowired private AdminIvrStatFeeService adminIvrStatFeeService;
	@Autowired private AdminSpService adminSpService;
	@Autowired private AdminUserService adminUserService;
	
	@RequestMapping("/ivrStatList")
	public String ivrStatList(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		date.setDate(date.getDate()-1);
		
		StringBuilder condition = new StringBuilder(" where ");
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("开始时间")) {
			startTime = DateUtil.formatDate(date);
		}
		condition.append(" dt >= '"+startTime+"'");
		url.append("&txtDateStart="+startTime);
		
		String endTime = request.getParameter("txtDateEnd");
		if(endTime==null||endTime.length()==0||endTime.equals("结束时间")) {
			endTime = DateUtil.formatDate(date);
		}
		condition.append(" and dt <= '"+endTime+"'");
		url.append("&txtDateEnd="+endTime);
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and gatewayid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and gatewayid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and channel = "+cpId);
		}
		
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"));
		url.append("&provinceId="+provinceId);
		if(provinceId>0) {
			condition.append(" and province = "+provinceId);
		}
		
		condition.append(" order by dt desc");
		
		List<AdminIvrStatFeeBean> statList = adminIvrStatFeeService.findStatByCondition(condition.toString());
		
		AdminIvrStatFeeBean all = new AdminIvrStatFeeBean();
		
		
		for (AdminIvrStatFeeBean adminIvrStatFeeBean : statList) {
			all.setCalluser(all.getCalluser()+adminIvrStatFeeBean.getCalluser());
			all.setCallnum(all.getCallnum()+adminIvrStatFeeBean.getCallnum());
			all.setCalltime(all.getCalltime()+adminIvrStatFeeBean.getCalltime());
			all.setCpcalluser(all.getCpcalluser()+adminIvrStatFeeBean.getCpcalluser());
			all.setCpcallnum(all.getCpcallnum()+adminIvrStatFeeBean.getCallnum());
			all.setCalltime(all.getCpcalltime()+adminIvrStatFeeBean.getCpcalltime());
		}
		
		
		request.setAttribute("all", all);
		
		
		int totalCount = statList.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<AdminIvrStatFeeBean> pageBean = new PageBean<AdminIvrStatFeeBean>(totalCount, currentPage,20);
		
		
		
		List<AdminIvrStatFeeBean> resultStatList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), statList);
		
		for (AdminIvrStatFeeBean adminIvrStatFeeBean : resultStatList) {
			adminIvrStatFeeBean.setAvgtime((float)adminIvrStatFeeBean.getCalltime()/adminIvrStatFeeBean.getCalluser());
			adminIvrStatFeeBean.setEverytime((float)adminIvrStatFeeBean.getCalltime()/adminIvrStatFeeBean.getCallnum());
			adminIvrStatFeeBean.setCpavgtime((float)adminIvrStatFeeBean.getCpcalltime()/adminIvrStatFeeBean.getCpcalluser());
			adminIvrStatFeeBean.setCpeverytime((float)adminIvrStatFeeBean.getCpcalltime()/adminIvrStatFeeBean.getCpcallnum());
			
			AdminSpBean spBean = adminSpService.findAdminSpById(adminIvrStatFeeBean.getSpid());
			
			adminIvrStatFeeBean.setSpcode(spBean.getSpcode());
			adminIvrStatFeeBean.setIsp(spBean.getSpisp());
			
			adminIvrStatFeeBean.setProvinceName(ProvinceService.getProvince(adminIvrStatFeeBean.getProvince()).getProvince());
			
			
			AdminUserBean userBean = adminUserService.findById(adminIvrStatFeeBean.getCpid());
			if(userBean!=null){				
				if(userBean.getCpname()!=null){				
					adminIvrStatFeeBean.setCpname(userBean.getCpname());
				}else{
					adminIvrStatFeeBean.setCpname("");
				}
			}else{
				adminIvrStatFeeBean.setCpname("");
			}
		}
		pageBean.setResult(resultStatList);
		
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		return "ivrStatList";
	}
	
	
	
	
	@RequestMapping("/ivrHourStatList")
	public String ivrHourStatList(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		
		StringBuilder condition = new StringBuilder(" where ");
		
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("选择日期")) {
			startTime = DateUtil.formatDate(date);
		}
		url.append("&txtDateStart="+startTime);
		//String startTime = DateUtil.formatDate(date);
		condition.append(" dt like '"+startTime+"%'");
		
		
		
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and gatewayid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and gatewayid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and channel = "+cpId);
		}
		
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"));
		url.append("&provinceId="+provinceId);
		if(provinceId>0) {
			condition.append(" and province = "+provinceId);
		}
		
		condition.append(" order by dt desc");
		
		List<AdminIvrStatFeeBean> statList = adminIvrStatFeeService.findHourStatByCondition(condition.toString());
		
		AdminIvrStatFeeBean all = new AdminIvrStatFeeBean();
		
		
		for (AdminIvrStatFeeBean adminIvrStatFeeBean : statList) {
			all.setCalluser(all.getCalluser()+adminIvrStatFeeBean.getCalluser());
			all.setCallnum(all.getCallnum()+adminIvrStatFeeBean.getCallnum());
			all.setCalltime(all.getCalltime()+adminIvrStatFeeBean.getCalltime());
			all.setCpcalluser(all.getCpcalluser()+adminIvrStatFeeBean.getCpcalluser());
			all.setCpcallnum(all.getCpcallnum()+adminIvrStatFeeBean.getCallnum());
			all.setCalltime(all.getCpcalltime()+adminIvrStatFeeBean.getCpcalltime());
		}
		
		
		request.setAttribute("all", all);
		
		
		int totalCount = statList.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<AdminIvrStatFeeBean> pageBean = new PageBean<AdminIvrStatFeeBean>(totalCount, currentPage,20);
		
		
		
		List<AdminIvrStatFeeBean> resultStatList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), statList);
		
		for (AdminIvrStatFeeBean adminIvrStatFeeBean : resultStatList) {
			adminIvrStatFeeBean.setAvgtime((float)adminIvrStatFeeBean.getCalltime()/adminIvrStatFeeBean.getCalluser());
			adminIvrStatFeeBean.setEverytime((float)adminIvrStatFeeBean.getCalltime()/adminIvrStatFeeBean.getCallnum());
			adminIvrStatFeeBean.setCpavgtime((float)adminIvrStatFeeBean.getCpcalltime()/adminIvrStatFeeBean.getCpcalluser());
			adminIvrStatFeeBean.setCpeverytime((float)adminIvrStatFeeBean.getCpcalltime()/adminIvrStatFeeBean.getCpcallnum());
			
			AdminSpBean spBean = adminSpService.findAdminSpById(adminIvrStatFeeBean.getSpid());
			
			adminIvrStatFeeBean.setSpcode(spBean.getSpcode());
			adminIvrStatFeeBean.setIsp(spBean.getSpisp());
			
			adminIvrStatFeeBean.setProvinceName(ProvinceService.getProvince(adminIvrStatFeeBean.getProvince()).getProvince());
			
			
			AdminUserBean userBean = new AdminUserBean();
			userBean = adminUserService.findById(adminIvrStatFeeBean.getCpid());
			if(userBean!=null){
				adminIvrStatFeeBean.setCpname(userBean.getCpname());				
			}else{
				adminIvrStatFeeBean.setCpname("");			
			}
		}
		pageBean.setResult(resultStatList);
		
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		return "ivrHourStatList";
	}
}
